package com.wlc.io;

import java.io.*;

/**
 * @author 王立朝 wanglichao@champion-credit.com
 * @description Buffered缓存流
 * @date 2021-07-10 08:56
 */
public class BufferedIoDemo {

    private static final String FILE_PATH = "/Users/wanglichao/Desktop/temp/a.txt";
    private static final String FILE_JAVA_PATH = "/Users/wanglichao/Desktop/temp/BufferedIoDemo.java";

    /**
     * 使用缓存字符输入流bufferedReader ，每次可以读取一行数据；
     * 使用缓存流的好处是，每次可以读取多个数据到内存中，减少了和磁盘的交互次数；
     * 字节流每读取一个字节都要和磁盘进行一次交互
     */
    public void bufferedReader() {
        try (FileReader fileReader = new FileReader(FILE_PATH);
             BufferedReader bufferedReader = new BufferedReader(fileReader);) {
            //BufferedReader bufferedReader = new BufferedReader(new FileReader(""));
            while (true) {
                String line = bufferedReader.readLine();
                if (null == line) {
                    break;
                }
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用PrintWriter 字符缓存输出流，每次输出一行
     */
    public void bufferWriter() {
        try (FileWriter fileWriter = new FileWriter(FILE_PATH);
             PrintWriter printWriter = new PrintWriter(fileWriter)) {
            printWriter.println("我是一个兵");
            printWriter.flush();
            printWriter.println("爱着老百姓");
            printWriter.flush();
            printWriter.println("在上海参加工作");
            printWriter.flush();
            printWriter.println("每个月工资2万5");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 清空Java文件中的注释//
     */
    public void clear() {
        StringBuffer sb = new StringBuffer();
        // 使用缓存输入流BufferReader 读取数据，需要建立FileReader的基础上
        try (FileReader fileReader = new FileReader(FILE_JAVA_PATH);
             BufferedReader bufferedReader = new BufferedReader(fileReader)) {
            while (true) {
                String line = bufferedReader.readLine();
                // 读取每一行的数据
                System.out.println(line);
                if (null == line) {
                    break;
                }
                // 判断每一行是否以// 开始;如果不是以// 开始的就写入到Stringbuffer 中
                if (!line.trim().startsWith("//")) {
                    sb.append(line).append("\t\n");
                }
            }
            System.out.println("读取完毕，数据为："+sb.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try(FileWriter fileWriter = new FileWriter(FILE_PATH);
        PrintWriter printWriter = new PrintWriter(fileWriter)){
            System.out.println("要读取的数据为："+sb.toString());
           printWriter.println(sb.toString());
           printWriter.flush();
           // 数据写入完毕
            System.out.println("数据写入完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        BufferedIoDemo bufferedIoDemo = new BufferedIoDemo();
        bufferedIoDemo.clear();
    }
}
